package com.asdhawaii.otello.players;

import com.asdhawaii.otello.board.Direction;
import com.asdhawaii.otello.board.Move;
import com.asdhawaii.otello.board.Square;
import com.asdhawaii.otello.board.SquareContents;
import java.util.List;

/**
 * An AI designed to play Otello.
 * 
 * @author Branden Ogata
 *
 */

public abstract class AbstractOtelloAi
{
  /**
   * The color of the pieces that this AI will play.
   * 
   */
  private SquareContents color;
  
  /**
   * Creates a new OtelloAi with the given color to play.
   * 
   * @param color The SquareContents indicating the color of pieces that the new OtelloAi will use.
   * 
   */
  
  public AbstractOtelloAi(SquareContents color)
  {
    this.color = color;
  }
  
  /**
   * Returns the color of the pieces for this OtelloAi.
   * 
   * @return this.color A SquareContents equal to the color of the pieces for this OtelloAi.
   * 
   */
  
  public SquareContents getColor()
  {
    return this.color;
  }
  
  /**
   * Sets the color of the pieces for this OtelloAi.
   * 
   * @param color The SquareContents indicating the color of the pieces this OtelloAi should play.
   * 
   */
  
  public void setColor(SquareContents color)
  {
    this.color = color;
  }
  
  /**
   * Indicates if a move in a particular direction is valid.
   * 
   * @param board The Square[][] representing the board.
   * @param x The int equal to the x coordinate of the current Square to examine.
   * @param y The int equal to the y coordinate of the current Square to examine.
   * @param direction The Direction to move in.
   * @param stack The String representing the pieces thus far in this line of inquiry.
   * 
   * @return A boolean that is true if the move is valid,
   *                           false otherwise.
   *                           
   */
  
  protected final boolean isValidMove(Square[][] board, int x, int y, 
                                      Direction direction, String stack)
  {
    boolean valid = false;
    
    // If the coordinates are invalid or the square is empty, then the move is invalid
    if ((y < 0) || (board.length <= y) || (x < 0) || (board[y].length <= x) || 
        (board[y][x].getContents().equals(SquareContents.EMPTY)))
    {
      valid = false;
    }
    // If the square is the same as the start of the stack 
    else if ((board[y][x].getContents().toString().equals(stack.substring(0, 1))))   
    {
      // If opposing pieces make up the rest of the stack then the flip is valid
      valid = !(stack.substring(1).contains(stack.substring(0, 1)) || (stack.length() < 2)); 
//      System.out.println(stack);
    }
    // Else continue recursion
    else
    {
      valid = isValidMove(board, x + direction.getDeltaX(), y + direction.getDeltaY(), 
                          direction, stack + board[y][x].getContents().toString());
    }
    
    return valid;
  }
  
  /**
   * Returns the move that this OtelloAi will make given a board and a list of valid moves.
   * 
   * @param board The Square[][] representing the Otello board.
   * @param moves The List<Move> containing the valid moves for the given board.
   * 
   * @return selection A Move containing the coordinates of the Square to move to.
   * 
   */
  
  public abstract Move getMove(Square[][] board, List<Move> moves);  
}
